package org.community.loginsvc.service.checkservice;

import com.sun.istack.internal.NotNull;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import org.community.loginsvc.dao.VisitorMapper;
import org.community.loginsvc.entites.Visitor;
import org.community.loginsvc.entites.VisitorExample;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(isolation = Isolation.REPEATABLE_READ)
@RequiredArgsConstructor
@Data
//账号密码确认服务
public class CheckServiceImpl implements CheckService{

    @NotNull
    private VisitorMapper visitorMapper;


    //查一下有无该用户
    @Override
    public int accountSearchAndAuth(Visitor visitor) {
        VisitorMapper visitorMapper = this.getVisitorMapper();
        VisitorExample example = new VisitorExample();
        example.createCriteria()
                .andUserNameEqualTo(visitor.getUserName())
                .andPasswordEqualTo(visitor.getPassword())
                ;
        int count = visitorMapper.countByExample(example);

        if (count == 0){
            return 404;
        }else if (count == 1){
            return 200;
        }else {
            return 500;
        }



    }

    //获取该用户信息
    @Override
    public Visitor accountSearch(Visitor visitor) {
        VisitorMapper visitorMapper = this.getVisitorMapper();
        VisitorExample example = new VisitorExample();
        example.createCriteria()
                .andUserNameEqualTo(visitor.getUserName())
        ;

        List<Visitor> users = visitorMapper.selectByExample(example);

        return users.get(0);
    }
}
